﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls.Primitives;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusColOnOffBar : FinPlusControl
    {
        public override string[] States { get { return new string[] { "Hidden" }; } }
        public override string[] TestStates { get { return new string[] { "Hidden", "IsValid" }; } }
        public event EventHandler<Function> ColOnOff;
        
        public bool Hidden 
        {
            get 
            { 
                return (bool)onOffToggle.IsChecked;
            } 
            set 
            { 
                onOffToggle.IsChecked = value; 
                ColOnOffsHide(value); 
            } 
        }

        private readonly IList<string> _onOffColumns;
       
        //construct
        public FinPlusColOnOffBar(string caption, IList<string> onOffColumns)
        {
            InitializeComponent();
            onOffToggle.Content = caption;
            _onOffColumns = onOffColumns;
            foreach (var col in _onOffColumns)
                AddButton(col);

            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IFunction command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.ColOn:
                    case Cmds.ColOff: ColOnOffs(command); break;
                    case Cmds.ColOnOffsHide: ColOnOffsHide(bool.Parse(command["Tag"].ToString())); break;
                    default: throw new Exception(string.Format("event not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = log.Log(Logger.Level.Warning, Id, Config, e.Message, e);
            }
        }

        //private
        private void AddButton(string name)
        {
            var button = new FinPlusColOnOffButton(name); 
            onOffBar.Children.Add(button);
            button.ColOnOff += ColOnOff_Click;
        }

        private void ColOnOffs(IFunction func)
        {
            ColOnOff.Invoke(this, (Function)func);
        }

        private void ColOnOffsHide(bool hidden)
        {
            if (hidden)
            {
                onOffBar.Width = 0;
                onOffBar.IsEnabled = false;
            }
            else
            {
                onOffBar.Width = Double.NaN;
                onOffBar.IsEnabled = true;
            }
        }

        //cmds
        private enum Cmds { ColOn, ColOff, ColOnOffsHide }

        //events
        private void ColOnOff_Click(object s, Function f){CmdExecute(f);}
        private void ColOnOffsHide_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.ColOnOffsHide.ToString(), ((ToggleButton)s).IsChecked.ToString()); }
    }
}
